/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.worlds.input;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.HashMap;
import net.worlds.Chat;
import net.worlds.Main;
import net.worlds.Util;
import net.worlds.ui.edge.EditorOverlay;
import net.worlds.ui.edge.MenuMain;


public class XKeyListener implements KeyListener{
    public static HashMap<Integer, String> keyChars = new HashMap<Integer, String>(256);
    public void keyTyped(KeyEvent e) {
        String chr = Character.toString(e.getKeyChar());
        if(chr.equals(".") && Main.chatBarActive == false && Main.getLocalPlayer() != null)
        {
            Main.chatBarText = "";
            Main.chatBarActive = true;
            return;
        }
        if("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+=-[]{}\\|;:'\",.<>/?`~ ".contains(chr))
        {
            if(Main.chatBarActive == true && Main.chatBarText.length() < 16384)
            {
                Main.chatBarText += chr;
            }
            else if(Main.isMenu() && ((MenuMain)Main.getView().getOverlay()).canChat())
            {
                Main.siteChatBarText += chr;
            }
        }
    }

    public void keyPressed(KeyEvent e) {
        InputState.addKey(e.getKeyCode());
        String chr = Character.toString(e.getKeyChar()).toLowerCase();
        keyChars.put(e.getKeyCode(), chr);
        if(e.getKeyCode() == KeyEvent.VK_BACK_SPACE && Main.chatBarText.length() > 0)
        {
            Main.chatBarText = Main.chatBarText.substring(0, Main.chatBarText.length() - 1);
        }
        if(Main.chatBarActive)
        {
            if(e.getKeyCode() == KeyEvent.VK_ESCAPE && Main.chatBarActive)
            {
                Main.chatBarText = "Press . to chat";
                Main.chatBarActive = false;
            }
            else if(e.getKeyCode() == KeyEvent.VK_ENTER && Main.chatBarActive)
            {
                Chat.send();
            }
            else if(e.getKeyCode() == KeyEvent.VK_V && e.isControlDown() && Main.chatBarActive)
            {
                String text = Util.getClipboardContents();
                Main.chatBarText += text;
            }
        }
        else if(Main.isMenu())
        {
            if(e.getKeyCode() == KeyEvent.VK_BACK_SPACE && Main.siteChatBarText.length() > 0)
            {
                Main.siteChatBarText = Main.siteChatBarText.substring(0, Main.siteChatBarText.length() - 1);
            }
            else if(e.getKeyCode() == KeyEvent.VK_ESCAPE)
            {
                Main.siteChatBarText = "";
            }
            else if(e.getKeyCode() == KeyEvent.VK_ENTER && Main.isMenu() && !Main.siteChatBarText.isEmpty())
            {
               ((MenuMain)Main.getView().getOverlay()).sendChat();
                Main.siteChatBarText = "";
            }
            else if(e.getKeyCode() == KeyEvent.VK_V && e.isControlDown() && !Main.siteChatBarText.isEmpty())
            {
                String text = Util.getClipboardContents();
                Main.siteChatBarText += text;
            }
        }
    }

    public static String getKeyChar(int c)
    {
        return keyChars.get(c);
    }

    public void keyReleased(KeyEvent e) {
        InputState.removeKey(e.getKeyCode());
    }

}
